package com.jiantong.version.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.version.impl.dao.DictionaryDao;
import com.jiantong.version.model.Dictionary;
import com.jiantong.version.service.DictionaryReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 25/8/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class DictionaryReadServiceImpl implements DictionaryReadService{


    private final DictionaryDao dictionaryDao;

    @Autowired
    public DictionaryReadServiceImpl(DictionaryDao dictionaryDao) {
        this.dictionaryDao = dictionaryDao;
    }

    /**
     * 根据类型查询字典
     *
     * @param type 类型
     * @return List<Dictionary>
     */
    @Override
    public Response<List<Dictionary>> findListByType(Integer type) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();

            criteria.put("type",type);

            return Response.ok(dictionaryDao.findListByType(criteria));
        }catch (Exception e){
            log.error("failed to find list by type, case={}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find list by type");
        }
    }

    /**
     * 根据类型查询字典
     *
     * @return List<Dictionary>
     */
    @Override
    public Response<List<Dictionary>> findAll() {
        try{
            return Response.ok(dictionaryDao.findListByType(null));
        }catch (Exception e){
            log.error("failed to find list, case={}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find list");
        }
    }
}
